<template>
  <div class="container">
    <a-form>
      <a-row :gutter="16">
        <a-col :span="6">
          <a-form-item label="U9姓名">
            <a-input allow-clear v-model="state.sd.name" placeholder="请输入U9姓名" />
          </a-form-item>
        </a-col>
        <a-col :span="6">
          <a-space>
            <a-button type="primary" @click="searchRymp">
              <template #icon><icon-search /></template>查询
            </a-button>
            <a-button type="primary" @click="addRymp">
              <template #icon><icon-plus-circle /></template>添加
            </a-button>
          </a-space>
        </a-col>
      </a-row>
    </a-form>

    <a-table :bordered="{cell:true}" :pagination="false" stripe :data="state.listObj.list">
      <template #columns>
        <a-table-column title="u9用户id" data-index="uId"></a-table-column>
        <a-table-column title="u9用户code" data-index="uCode"></a-table-column>
        <a-table-column title="u9用户name" data-index="uName"></a-table-column>
        <a-table-column title="1688账户" data-index="aAccount"></a-table-column>
        <a-table-column title="1688员工姓名" data-index="aName"></a-table-column>
        <a-table-column title="操作" width="200">
          <template #cell="{ record }">
            <a-space>
              <a-button type="primary" size="small" @click="editRymp(record.id)"><template #icon><icon-edit /></template>修改</a-button>
              <a-popconfirm content="确认删除吗?" @ok="deleteRymp(record.id)">
                <a-button type="primary" status="danger" size="small"><template #icon><icon-delete /></template>删除</a-button>
              </a-popconfirm>
            </a-space>
          </template>
        </a-table-column>
      </template>
    </a-table>
    <a-pagination class="mt10" show-total :total="state.listObj.total" @change="loadRymp" @page-size-change="loadRymp" v-model:current="state.sd.pageNum" v-model:page-size="state.sd.pageSize"></a-pagination>
    <a-modal :title="state.popupTitle" v-model:visible="state.entityDetailVisible" :footer="false">
      <Detail v-if="state.entityDetailVisible" v-model="state.entityDetailVisible" :entityId="state.popupEntityId"></Detail>
    </a-modal>
  </div>
</template>

<script lang="ts" setup>
import axios from "axios";
import { Message } from '@arco-design/web-vue'
import mittBus from "@/utils/route-listener"
import {reactive, ref, onMounted, onUnmounted, defineAsyncComponent} from "vue";
// 引入组件
const Detail = defineAsyncComponent(() => import('@/views/basic/rymp/detail.vue'));

const state = reactive({
  sd: {
    name : '',
    pageNum: 1,
    pageSize: 15
  },
  listObj: {},

  entityDetailVisible : false,
  popupTitle : '',
  popupEntityId : 0,
})

const searchRymp = ()=>{
  state.sd.pageNum = 1;
  loadRymp();
}
const loadRymp = ()=>{
  axios.post('/renyuanmapping/page', state.sd).then(res => {
    const { data } = res
    state.listObj = data
  })
}
const addRymp = ()=>{
  state.popupEntityId = 0;
  state.popupTitle= "新增用户映射"
  state.entityDetailVisible= true
}
const editRymp = (id)=>{
  state.popupEntityId = id;
  state.popupTitle= "修改用户映射"
  state.entityDetailVisible= true
}
const deleteRymp = (id)=>{
  axios.post('/renyuanmapping/delete', {id : id}).then(res => {
    const { code, msg } = res
    if(code === '200') {
      Message.success(msg)
      loadRymp();
    }
  })
}
onMounted(()=>{
  loadRymp();
  mittBus.on("rympadd", ()=>{
    state.sd.pageNum = 1;
    loadRymp();
  })
  mittBus.on("rympedit", ()=>{
    loadRymp();
  })
})
onUnmounted(()=>{
  mittBus.off("rympadd", ()=>{})
  mittBus.off("rympedit",()=>{})
})
</script>

<style scoped>

</style>
